package com.yunbiao.ybbusstopplate.refactoring.ui.dalian;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;
import com.yunbiao.ybbusstopplate.R;
import com.yunbiao.ybbusstopplate.databinding.ItemNodeSiteUpBinding;

import java.util.List;

public class DalianSiteAdapter extends RecyclerView.Adapter<DalianSiteAdapter.Holder> {

    private final int COLOR_BLUE;
    private final int COLOR_RED;
    private final int COLOR_GRAY;
    private final int COLOR_BLACK;
    private final List<MixSite> dataList;

    public DalianSiteAdapter(List<MixSite> list) {
        this.dataList = list;
        this.COLOR_RED = Color.RED;
        this.COLOR_BLUE = Color.parseColor("#4472C3");
        this.COLOR_GRAY = Color.GRAY;
        this.COLOR_BLACK = Color.BLACK;
    }

    private static final String TAG = "DalianSiteAdapter";
    @NonNull
    @Override
    public DalianSiteAdapter.Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_node_site_up,viewGroup,false);
        Holder holder = new Holder(view);
        updateSize(holder,dataList.size());
        return holder;
    }

    private int mIndex = -1;
    private String mInOut = "01";
    private String mUpDown = "01";
    private int mPosition;

    public void broadSite(int index, String inOut, String upDown) {
        this.mIndex = index;
        this.mInOut = inOut;
        this.mUpDown = upDown;

        MixSite mMixSite = null;
        for (MixSite mixSite : dataList) {
            if(TextUtils.equals("01",this.mUpDown) && mixSite.isUp() && mixSite.getUpIndex() == this.mIndex){
                mMixSite = mixSite;
                break;
            } else if(TextUtils.equals("02",this.mUpDown) && mixSite.isDown() && mixSite.getDownIndex() == this.mIndex){
                mMixSite = mixSite;
                break;
            }
        }
        if(mMixSite != null){
            mPosition = dataList.indexOf(mMixSite);
        } else {
            mPosition = -1;
        }

        if(mPosition == -1){
            return;
        }

        Log.d(TAG, "broadSite: " + mIndex + " --- " + mInOut + " --- " + mUpDown + " --- " + mPosition + " --- " + (mMixSite != null ? mMixSite.getChName() : "NULL"));

        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull DalianSiteAdapter.Holder holder, int position, @NonNull List<Object> payloads) {
        MixSite site = dataList.get(position);
        if(payloads.isEmpty()){

            holder.binding.tvSite.setText(site.getChName());

            //判断上下行显示箭头
            if (site.isUp() && site.isDown()) {
                holder.binding.ivUp.setVisibility(View.VISIBLE);
                holder.binding.ivDown.setVisibility(View.VISIBLE);
            } else if(site.isUp()){
                holder.binding.ivUp.setVisibility(View.VISIBLE);
                holder.binding.ivDown.setVisibility(View.INVISIBLE);
            } else if(site.isDown()){
                holder.binding.ivUp.setVisibility(View.INVISIBLE);
                holder.binding.ivDown.setVisibility(View.VISIBLE);
            } else {
                holder.binding.ivUp.setVisibility(View.INVISIBLE);
                holder.binding.ivDown.setVisibility(View.INVISIBLE);
            }

            //上行
            if(TextUtils.equals("01",this.mUpDown)){
                //旋转动画箭头
                holder.binding.aavRight.setRotation(0f);
                holder.binding.aavLeft.setRotation(0f);

                //显示方向
                holder.binding.ivUp.setImageResource(R.mipmap.icon_up_green);
                holder.binding.ivDown.setImageResource(R.mipmap.icon_down_red);

                //如果当前站是上行
                if(site.isUp()){
                    holder.binding.tvSite.setTextColor(COLOR_BLACK);

                    //如果是当前站
                    if(site.getUpIndex() == mIndex){
                        //到站左灰右蓝
                        if("01".equals(mInOut)){
                            holder.binding.tvSite.setTextColor(COLOR_BLUE);
                            holder.binding.vLeftLine.setBackgroundColor(COLOR_GRAY);
                            holder.binding.vRightLine.setBackgroundColor(COLOR_BLUE);

                            holder.binding.aavLeft.setVisibility(View.GONE);
                            holder.binding.aavRight.setVisibility(View.GONE);
                        }
                        //下一站左蓝右蓝
                        else {
                            holder.binding.tvSite.setTextColor(COLOR_RED);
                            holder.binding.vLeftLine.setBackgroundColor(COLOR_BLUE);
                            holder.binding.vRightLine.setBackgroundColor(COLOR_BLUE);

                            holder.binding.aavLeft.setVisibility(View.VISIBLE);
                            holder.binding.aavRight.setVisibility(View.GONE);
                        }
                    }
                    //如果是前站，则全灰
                    else if(site.getUpIndex() < mIndex){
                        holder.binding.tvSite.setTextColor(COLOR_GRAY);
                        holder.binding.vLeftLine.setBackgroundColor(COLOR_GRAY);
                        holder.binding.vRightLine.setBackgroundColor(COLOR_GRAY);

                        holder.binding.aavLeft.setVisibility(View.GONE);
                        holder.binding.aavRight.setVisibility(View.GONE);
                    }
                    //如果是后站则全蓝
                    else if(site.getUpIndex() > mIndex){
                        holder.binding.tvSite.setTextColor(COLOR_BLACK);
                        holder.binding.vLeftLine.setBackgroundColor(COLOR_BLUE);
                        holder.binding.vRightLine.setBackgroundColor(COLOR_BLUE);

                        holder.binding.aavLeft.setVisibility(View.GONE);
                        holder.binding.aavRight.setVisibility(View.GONE);
                    }
                    //未知
                    else {
                        holder.binding.tvSite.setTextColor(COLOR_BLACK);
                        holder.binding.vLeftLine.setBackgroundColor(COLOR_BLUE);
                        holder.binding.vRightLine.setBackgroundColor(COLOR_BLUE);

                        holder.binding.aavLeft.setVisibility(View.GONE);
                        holder.binding.aavRight.setVisibility(View.GONE);
                    }
                }
                //如果不是上行则字灰
                else {
                    holder.binding.tvSite.setTextColor(COLOR_GRAY);

                    //根据position判断，小于则全灰，大于则全蓝，不会有position等于的情况
                    if(position <= mPosition){
                        holder.binding.vLeftLine.setBackgroundColor(COLOR_GRAY);
                        holder.binding.vRightLine.setBackgroundColor(COLOR_GRAY);
                    } else {
                        holder.binding.vLeftLine.setBackgroundColor(COLOR_BLUE);
                        holder.binding.vRightLine.setBackgroundColor(COLOR_BLUE);
                    }

                    holder.binding.aavLeft.setVisibility(View.GONE);
                    holder.binding.aavRight.setVisibility(View.GONE);
                }
            }
            //下行
            else {
                //旋转动画箭头
                holder.binding.aavRight.setRotation(180f);
                holder.binding.aavLeft.setRotation(180f);

                //显示方向
                holder.binding.ivUp.setImageResource(R.mipmap.icon_up_red);
                holder.binding.ivDown.setImageResource(R.mipmap.icon_down_green);

                if(site.isDown()){
                    holder.binding.tvSite.setTextColor(COLOR_BLACK);

                    //如果是当前站
                    if(site.getDownIndex() == mIndex){
                        //到站右灰左蓝
                        if("01".equals(mInOut)){
                            holder.binding.tvSite.setTextColor(COLOR_BLUE);
                            holder.binding.vLeftLine.setBackgroundColor(COLOR_BLUE);
                            holder.binding.vRightLine.setBackgroundColor(COLOR_GRAY);

                            holder.binding.aavLeft.setVisibility(View.GONE);
                            holder.binding.aavRight.setVisibility(View.GONE);
                        } else {
                            holder.binding.tvSite.setTextColor(COLOR_RED);
                            holder.binding.vLeftLine.setBackgroundColor(COLOR_BLUE);
                            holder.binding.vRightLine.setBackgroundColor(COLOR_BLUE);

                            holder.binding.aavLeft.setVisibility(View.GONE);
                            holder.binding.aavRight.setVisibility(View.VISIBLE);
                        }
                    }
                    //如果是前站，则全灰
                    else if(site.getDownIndex() < mIndex){
                        holder.binding.tvSite.setTextColor(COLOR_GRAY);
                        holder.binding.vLeftLine.setBackgroundColor(COLOR_GRAY);
                        holder.binding.vRightLine.setBackgroundColor(COLOR_GRAY);

                        holder.binding.aavLeft.setVisibility(View.GONE);
                        holder.binding.aavRight.setVisibility(View.GONE);
                    }
                    //如果是后站则全蓝
                    else if(site.getDownIndex() > mIndex){
                        holder.binding.tvSite.setTextColor(COLOR_BLACK);
                        holder.binding.vLeftLine.setBackgroundColor(COLOR_BLUE);
                        holder.binding.vRightLine.setBackgroundColor(COLOR_BLUE);

                        holder.binding.aavLeft.setVisibility(View.GONE);
                        holder.binding.aavRight.setVisibility(View.GONE);
                    }
                    //未知
                    else {
                        holder.binding.tvSite.setTextColor(COLOR_BLACK);
                        holder.binding.vLeftLine.setBackgroundColor(COLOR_BLUE);
                        holder.binding.vRightLine.setBackgroundColor(COLOR_BLUE);

                        holder.binding.aavLeft.setVisibility(View.GONE);
                        holder.binding.aavRight.setVisibility(View.GONE);
                    }
                } else {
                    holder.binding.tvSite.setTextColor(COLOR_GRAY);

                    //根据position判断，大于则全灰，小于则全蓝，不会有position等于的情况
                    if(position <= mPosition){
                        holder.binding.vLeftLine.setBackgroundColor(COLOR_BLUE);
                        holder.binding.vRightLine.setBackgroundColor(COLOR_BLUE);
                    } else {
                        holder.binding.vLeftLine.setBackgroundColor(COLOR_GRAY);
                        holder.binding.vRightLine.setBackgroundColor(COLOR_GRAY);
                    }

                    holder.binding.aavLeft.setVisibility(View.GONE);
                    holder.binding.aavRight.setVisibility(View.GONE);
                }
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull DalianSiteAdapter.Holder holder, int i) {

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    private void updateSize(Holder holder,int spanCount){

        int lineHeight;
        int dotSize;
        int arrowWidth;
        int textSize;
        int textColumnCount = 8;
        int arrowAnimHeight;
        if(spanCount >= 40){
            lineHeight = ConvertUtils.dp2px(32f);
            dotSize = ConvertUtils.dp2px(32f);
            arrowWidth = ConvertUtils.dp2px(16f);
            textSize = ConvertUtils.sp2px(16f);
            textColumnCount = 13;
        } else if(spanCount >= 30){
            lineHeight = ConvertUtils.dp2px(36f);
            dotSize = ConvertUtils.dp2px(36f);
            arrowWidth = ConvertUtils.dp2px(18f);
            textSize = ConvertUtils.sp2px(19f);
            textColumnCount = 10;
        } else if(spanCount >= 20){
            lineHeight = ConvertUtils.dp2px(40f);
            dotSize = ConvertUtils.dp2px(40f);
            arrowWidth = ConvertUtils.dp2px(20f);
            textSize = ConvertUtils.sp2px(22f);
            textColumnCount = 9;
        } else {
            lineHeight = ConvertUtils.dp2px(42f);
            dotSize = ConvertUtils.dp2px(42f);
            arrowWidth = ConvertUtils.dp2px(21f);
            textSize = ConvertUtils.sp2px(24f);
            textColumnCount = 9;
        }

        ViewGroup.LayoutParams layoutParams = holder.binding.vLeftLine.getLayoutParams();
        layoutParams.height = lineHeight;
        holder.binding.vLeftLine.setLayoutParams(layoutParams);

        layoutParams = holder.binding.vRightLine.getLayoutParams();
        layoutParams.height = lineHeight;
        holder.binding.vRightLine.setLayoutParams(layoutParams);

        layoutParams = holder.binding.vCenterDot.getLayoutParams();
        layoutParams.width = dotSize;
        layoutParams.height = dotSize;
        holder.binding.vCenterDot.setLayoutParams(layoutParams);

        layoutParams = holder.binding.ivDown.getLayoutParams();
        layoutParams.width = arrowWidth;
        holder.binding.ivDown.setLayoutParams(layoutParams);

        layoutParams = holder.binding.ivUp.getLayoutParams();
        layoutParams.width = arrowWidth;
        holder.binding.ivUp.setLayoutParams(layoutParams);

        holder.binding.tvSite.setTextSize(textSize);
        holder.binding.tvSite.setTextColumnCount(textColumnCount);
    }

    public static class Holder extends RecyclerView.ViewHolder {

        public final ItemNodeSiteUpBinding binding;

        public Holder(@NonNull View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
        }
    }

    public static class MixSite{
        private boolean isUp;
        private boolean isDown;

        private int upIndex;
        private int downIndex;
        private String chName;
        private String enName;

        public boolean isUp() {
            return isUp;
        }

        public void setUp(boolean up) {
            isUp = up;
        }

        public boolean isDown() {
            return isDown;
        }

        public void setDown(boolean down) {
            isDown = down;
        }

        public int getUpIndex() {
            return upIndex;
        }

        public void setUpIndex(int upIndex) {
            this.upIndex = upIndex;
        }

        public int getDownIndex() {
            return downIndex;
        }

        public void setDownIndex(int downIndex) {
            this.downIndex = downIndex;
        }

        public String getChName() {
            return chName;
        }

        public void setChName(String chName) {
            this.chName = chName;
        }

        public String getEnName() {
            return enName;
        }

        public void setEnName(String enName) {
            this.enName = enName;
        }

        @NonNull
        @Override
        public String toString() {
            return "\n"
                    + chName
                    + " --- "
                    + (isUp ? "上" : "") + (isDown ? "下" : "")
                    + " --- "
                    + (isUp ? "上[" + upIndex + "]" : "") + (isDown ? "下[" + downIndex + "]" : "")
                    ;
        }
    }

}
